// LunarCalendar.m 
// SYEdu_Company 
// 
// Created by 尚朋 on 2019/5/23. 
// Copyright © 2019年 qhths. All rights reserved. 
// 
// @class  LunarCalendar
// abstract  <#摘要#>
// description  <#描述#>

#define baseYear  1901

#import "LunarCalendar.h"

@interface LunarCalendar ()

@property (nonatomic, strong) NSCalendar *gregorianCalendar;
@property (nonatomic, strong) NSArray *sectionalTermMap;
@property (nonatomic, strong) NSArray *sectionalTermYear;
@property (nonatomic, strong) NSArray *principleTermMap;
@property (nonatomic, strong) NSArray *principleTermYear;
@property (nonatomic, strong) NSArray *principleTermNames;
@property (nonatomic, strong) NSArray *sectionalTermNames;


@end

@implementation LunarCalendar

static LunarCalendar * lunar = nil;

+(instancetype)shareManager{
    if (lunar == nil) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            lunar = [[self alloc] init];
        });
    }
    return lunar;
}

/**
 获取节气

 @param gregorianYear <#gregorianYear description#>
 @param gregorianMonth <#gregorianMonth description#>
 @param gregorianDate <#gregorianDate description#>
 @return <#return value description#>
 */
- (NSString *)getSolartermsMsg:(NSInteger)gregorianYear
                gregorianMonth:(NSInteger)gregorianMonth
                 gregorianDate:(NSInteger)gregorianDate{
    int sectionalTerm = [self sectionalTerm:gregorianYear mother:gregorianMonth];
    int principleTerm = [self principleTerm:gregorianYear mother:gregorianMonth];
    NSString *str = @"";
    if (gregorianDate == sectionalTerm) {
        str = self.sectionalTermNames[gregorianMonth - 1];
    } else if (gregorianDate == principleTerm) {
        str = self.principleTermNames[gregorianMonth - 1];
    }
    return str;
}

- (int)sectionalTerm:(NSInteger)y mother:(NSInteger)m{
    if (y < 1901 || y > 2100){
        return 0;
    }
    NSInteger index = 0;
    NSInteger ry = y - baseYear + 1;
    while (ry >= [self.sectionalTermYear[m - 1][index] intValue]) {
        index++;
    }
    int term = [self.sectionalTermMap[m - 1][4 * index + ry % 4] intValue];
    if ((ry == 121) && (m == 4)){
        term = 5;
    }
    if ((ry == 132) && (m == 4)){
        term = 5;
    }
    if ((ry == 194) && (m == 6)){
        term = 6;
    }
    return term;
}
- (int)principleTerm:(NSInteger)y mother:(NSInteger)m{
    if (y < 1901 || y > 2100){
        return 0;
    }
    int index = 0;
    NSInteger ry = y - baseYear + 1;
    while (ry >= [self.principleTermYear[m - 1][index] intValue]) {
        index++;
    }
    int term = [self.principleTermMap[m - 1][4 * index + ry % 4] intValue];
    if ((ry == 171) && (m == 3)){
        term = 21;
    }
    if ((ry == 181) && (m == 5)){
        term = 21;
    }
    return term;
}
-(NSCalendar *)gregorianCalendar{
    if (!_gregorianCalendar) {
        _gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    }
    return _gregorianCalendar;
}
-(NSArray *)principleTermNames{
    if (!_principleTermNames) {
        _principleTermNames = @[@"大寒", @"雨水", @"春分", @"谷雨", @"小满", @"夏至", @"大暑", @"处暑", @"秋分", @"霜降", @"小雪", @"冬至"];
    }
    return _principleTermNames;
}
-(NSArray *)sectionalTermNames{
    if (!_sectionalTermNames) {
        _sectionalTermNames = @[@"小寒", @"立春", @"惊蛰", @"清明", @"立夏", @"芒种", @"小暑", @"立秋", @"白露", @"寒露", @"立冬", @"大雪"];
    }
    return _sectionalTermNames;
}
-(NSArray *)sectionalTermMap{
    if (!_sectionalTermMap) {
        _sectionalTermMap = @[
                              @[@(7), @(6), @(6), @(6), @(6), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(5), @(5), @(5), @(5), @(5), @(4), @(5), @(5)],
                              @[@(5), @(4), @(5), @(5), @(5), @(4), @(4), @(5), @(5), @(4), @(4), @(4), @(4), @(4), @(4), @(4), @(4), @(3), @(4), @(4), @(4), @(3), @(3), @(4), @(4), @(3), @(3), @(3)],
                              @[@(6), @(6), @(6), @(7), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(5)],
                              @[@(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(4), @(4), @(5), @(5), @(4), @(4), @(4), @(5), @(4), @(4), @(4), @(4), @(5)],
                              @[@(6), @(6), @(6), @(7), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(5)],
                              @[@(6), @(6), @(7), @(7), @(6), @(6), @(6), @(7), @(6), @(6), @(6), @(6), @(5), @(6), @(6), @(6), @(5), @(5), @(6), @(6), @(5), @(5), @(5), @(6), @(5), @(5), @(5), @(5), @(4), @(5), @(5), @(5), @(5)],
                              @[@(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(6), @(6), @(6), @(7), @(7)],
                              @[@(8), @(8), @(8), @(9), @(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(7)],
                              @[@(8), @(8), @(8), @(9), @(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(7)],
                              @[@(9), @(9), @(9), @(9), @(8), @(9), @(9), @(9), @(8), @(8), @(9), @(9), @(8), @(8), @(8), @(9), @(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(8)],
                              @[@(8), @(8), @(8), @(8), @(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(7)],
                              @[@(7), @(8), @(8), @(8), @(7), @(7), @(8), @(8), @(7), @(7), @(7), @(8), @(7), @(7), @(7), @(7), @(6), @(7), @(7), @(7), @(6), @(6), @(7), @(7), @(6), @(6), @(6), @(7), @(7)]
                              ];
    }
    return _sectionalTermMap;
}

- (NSArray *)sectionalTermYear{
    if (!_sectionalTermYear) {
        _sectionalTermYear = @[
                               @[@13, @49, @85, @117, @149, @185, @201, @250, @250],
                               @[@13, @45, @81, @117, @149, @185, @201, @250, @250],
                               @[@13, @48, @84, @112, @148, @184, @200, @201, @250],
                               @[@13, @45, @76, @108, @140, @172, @200, @201, @250],
                               @[@13, @44, @72, @104, @132, @168, @200, @201, @250],
                               @[@5, @33, @68, @96, @124, @152, @188, @200, @201],
                               @[@29, @57, @85, @120, @148, @176, @200, @201, @250],
                               @[@13, @48, @76, @104, @132, @168, @196, @200, @201],
                               @[@25, @60, @88, @120, @148, @184, @200, @201, @250],
                               @[@16, @44, @76, @108, @144, @172, @200, @201, @250],
                               @[@28, @60, @92, @124, @160, @192, @200, @201, @250],
                               @[@17, @53, @85, @124, @156, @188, @200, @201, @250]
                               ];
    }
    return _sectionalTermYear;
}
-(NSArray *)principleTermMap{
    if (!_principleTermMap) {
        _principleTermMap = @[
                              @[@21, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @20, @20, @20, @20, @20, @19, @20, @20, @20, @19, @19, @20],
                              @[@20, @19, @19, @20, @20, @19, @19, @19, @19, @19, @19, @19, @19, @18, @19, @19, @19, @18, @18, @19, @19, @18, @18, @18, @18, @18, @18, @18],
                              @[@21, @21, @21, @22, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @21, @20, @20, @20, @20, @19, @20, @20, @20, @20],
                              @[@20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @21, @20, @20, @20, @20, @19, @20, @20, @20, @19, @19, @20, @20, @19, @19, @19, @20, @20],
                              @[@21, @22, @22, @22, @21, @21, @22, @22, @21, @21, @21, @22, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @20, @20, @20, @21, @21],
                              @[@22, @22, @22, @22, @21, @22, @22, @22, @21, @21, @22, @22, @21, @21, @21, @22, @21, @21, @21, @21, @20, @21, @21, @21, @20, @20, @21, @21, @21],
                              @[@23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @22, @22, @22, @22, @23],
                              @[@23, @24, @24, @24, @23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @23],
                              @[@23, @24, @24, @24, @23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @23],
                              @[@24, @24, @24, @24, @23, @24, @24, @24, @23, @23, @24, @24, @23, @23, @23, @24, @23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @23],
                              @[@23, @23, @23, @23, @22, @23, @23, @23, @22, @22, @23, @23, @22, @22, @22, @23, @22, @22, @22, @22, @21, @22, @22, @22, @21, @21, @22, @22, @22],
                              @[@22, @22, @23, @23, @22, @22, @22, @23, @22, @22, @22, @22, @21, @22, @22, @22, @21, @21, @22, @22, @21, @21, @21, @22, @21, @21, @21, @21, @22]];
    }
    return _principleTermMap;
}
-(NSArray *)principleTermYear{
    if (!_principleTermYear) {
        _principleTermYear = @[
                               @[@13, @45, @81, @113, @149, @185, @201],
                               @[@21, @57, @93, @125, @161, @193, @201],
                               @[@21, @56, @88, @120, @152, @188, @200, @201],
                               @[@21, @49, @81, @116, @144, @176, @200, @201],
                               @[@17, @49, @77, @112, @140, @168, @200, @201],
                               @[@28, @60, @88, @116, @148, @180, @200, @201],
                               @[@25, @53, @84, @112, @144, @172, @200, @201],
                               @[@29, @57, @89, @120, @148, @180, @200, @201],
                               @[@17, @45, @73, @108, @140, @168, @200, @201],
                               @[@28, @60, @92, @124, @160, @192, @200, @201],
                               @[@16, @44, @80, @112, @148, @180, @200, @201],
                               @[@17, @53, @88, @120, @156, @188, @200, @201]
                               ];
    }
    return _principleTermYear;
}





+ (NSString *)getSolartermsMsgWithDate:(NSDate *)date{
    
    NSInteger year = [[[NSCalendar currentCalendar] components:NSCalendarUnitYear fromDate:date] year];
    NSInteger month = [[[NSCalendar currentCalendar] components:NSCalendarUnitMonth fromDate:date] month];
    NSInteger day = [[[NSCalendar currentCalendar] components:NSCalendarUnitDay fromDate:date] day];
    return [[LunarCalendar shareManager] getSolartermsMsg:year gregorianMonth:month gregorianDate:day];
}


+ (NSArray*)getChineseCalendarWithDate:(NSDate *)date{
    
    NSArray *chineseYears = [NSArray arrayWithObjects:
                             @"甲子",   @"乙丑", @"丙寅", @"丁卯",  @"戊辰",  @"己巳",  @"庚午",  @"辛未",  @"壬申",  @"癸酉",
                             @"甲戌",   @"乙亥",  @"丙子",  @"丁丑", @"戊寅",   @"己卯",  @"庚辰",  @"辛己",  @"壬午",  @"癸未",
                             @"甲申",   @"乙酉",  @"丙戌",  @"丁亥",  @"戊子",  @"己丑",  @"庚寅",  @"辛卯",  @"壬辰",  @"癸巳",
                             @"甲午",   @"乙未",  @"丙申",  @"丁酉",  @"戊戌",  @"己亥",  @"庚子",  @"辛丑",  @"壬寅",  @"癸丑",
                             @"甲辰",   @"乙巳",  @"丙午",  @"丁未",  @"戊申",  @"己酉",  @"庚戌",  @"辛亥",  @"壬子",  @"癸丑",
                             @"甲寅",   @"乙卯",  @"丙辰",  @"丁巳",  @"戊午",  @"己未",  @"庚申",  @"辛酉",  @"壬戌",  @"癸亥", nil];
    
    NSArray *chineseMonths=[NSArray arrayWithObjects:
                            @"正月", @"二月", @"三月", @"四月", @"五月", @"六月",
                            @"七月", @"八月", @"九月", @"十月", @"冬月", @"腊月", nil];
    
    
    NSArray *chineseDays=[NSArray arrayWithObjects:
                          @"初一", @"初二", @"初三", @"初四", @"初五", @"初六", @"初七", @"初八", @"初九", @"初十",
                          @"十一", @"十二", @"十三", @"十四", @"十五", @"十六", @"十七", @"十八", @"十九", @"二十",
                          @"廿一", @"廿二", @"廿三", @"廿四", @"廿五", @"廿六", @"廿七", @"廿八", @"廿九", @"三十",  nil];
    
    NSCalendar *localeCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierChinese];
    
    unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay;
    
    NSDateComponents *localeComp = [localeCalendar components:unitFlags fromDate:date];
    
    NSString *y_str = [chineseYears objectAtIndex:localeComp.year-1];
    NSString *m_str = [chineseMonths objectAtIndex:localeComp.month-1];
    NSString *d_str = [chineseDays objectAtIndex:localeComp.day-1];
    //    NSString *chineseCal_str =[NSString stringWithFormat: @"%@_%@_%@",y_str,m_str,d_str];
    
    NSArray * array = @[y_str,m_str,d_str];
    
    
    return array;
}







@end
